/*
 * @Author: mikey.zhaopeng 
 * @Date: 2019-01-09 20:34:03 
 * @Last Modified by: mikey.zhaopeng
 * @Last Modified time: 2019-01-09 23:38:24
 */

import 'package:flutter/material.dart';
import '../widgets/discount_item.dart' show DiscountItem;

class GameType extends StatefulWidget {
  @override
  _GameTypeState createState() => _GameTypeState();
}

class _GameTypeState extends State<GameType> {
  List<Widget> itemList;
  ScrollController _scrollController = new ScrollController();
  @override
  void initState() {
    super.initState();
    itemList = [
      DiscountItem(),
      DiscountItem(),
      DiscountItem(),
      DiscountItem(),
      DiscountItem(),
      DiscountItem(),
      DiscountItem(),
      DiscountItem(),
      DiscountItem(),
      DiscountItem(),
      DiscountItem(),
      DiscountItem(),
      DiscountItem(),
      DiscountItem(),
    ];
    _scrollController.addListener(() {
      if (_scrollController.position.pixels ==_scrollController.position.maxScrollExtent) {
        print('滑动到了最底部');
      }
    });
    print(itemList.length);
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: <Widget>[
          Container(
            height: 50.0,
            alignment: Alignment.centerLeft,
            child: ListView(
              scrollDirection: Axis.horizontal,
              padding:
                  const EdgeInsets.symmetric(vertical: 10.0, horizontal: 15.0),
              children: <Widget>[
                GameTypeItem(),
                GameTypeItem(),
                GameTypeItem(),
                GameTypeItem(),
                GameTypeItem(),
                GameTypeItem(),
                GameTypeItem(),
              ],
            ),
          ),
          // MyHomePage(),
          Expanded(
            flex: 1,
            child: Container(
              child: ListView.builder(
                controller: _scrollController,
                itemCount: itemList.length +1,
                itemBuilder: (BuildContext context, int index) {
                  if (index < itemList.length) {
                    return itemList[index];
                  }
                  return Text("加载更多");
                },
              ),
            ),
          ),
        ],
      ),
    );
  }
}

class GameTypeItem extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      alignment: Alignment.center,
      padding: const EdgeInsets.symmetric(horizontal: 20),
      decoration: BoxDecoration(
        color: Color(0xfff4fbfd),
        borderRadius: BorderRadius.all(Radius.circular(15)),
      ),
      child: Text("仙侠"),
    );
  }
}
